package com.tjmc.company.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tjmc.company.api.entity.JobCategoryEntity;
import com.tjmc.company.mapper.JobCategoryMapper;
import com.tjmc.company.service.JobCategoryService;
import com.tjmc.recruit.admin.api.entity.SysMenu;
import com.tjmc.recruit.common.core.util.R;
import jakarta.validation.constraints.NotNull;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 职位分类表
 *
 * @author recruit
 * @date 2024-11-29 15:24:34
 */
@Service
public class JobCategoryServiceImpl extends ServiceImpl<JobCategoryMapper, JobCategoryEntity> implements JobCategoryService {

    @Override
    public  List<Tree<Long>> treeCategories(Integer level) {
        TreeNodeConfig config = new TreeNodeConfig();
        config.setDeep(level);
        // 1. 查询全部行业信息
        List<TreeNode<Long>> collect = baseMapper
                .selectList(Wrappers.<JobCategoryEntity>lambdaQuery()
                        .orderByAsc(JobCategoryEntity::getSortOrder))
                .stream()
                .map(getNodeFunction())
                .toList();
        return TreeUtil.build(collect, null, config, ((treeNode, tree) -> {
            tree.setId(treeNode.getId());
            tree.setParentId(treeNode.getParentId());
            tree.setName(treeNode.getName());
        }));
    }

    @NotNull
    private Function<JobCategoryEntity, TreeNode<Long>> getNodeFunction() {
        return category -> {
            TreeNode<Long> node = new TreeNode<>();
            node.setId(category.getId());
            node.setName(category.getCategoryName());
            node.setParentId(category.getParentId());
            // 扩展属性
            Map<String, Object> extra = new HashMap<>();
            extra.put("level", category.getLevel());
            node.setExtra(extra);
            return node;
        };
    }
}
